package com.fengfeng;

import android.app.Activity;
import android.widget.Toast;
import com.nearme.game.sdk.GameCenterSDK;
import com.nearme.game.sdk.callback.ApiCallback;
import com.nearme.game.sdk.callback.GameExitCallback;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author xiaoguokeng
 * @Date 2024/9/4
 */
public class ChannelSDK implements ChannelLifeMethod{
    private PlatformCallback mCallback;
    private Activity mActivity;
    private static ChannelSDK mChannelSDK;
    private ChannelSDK(){}
    public static ChannelSDK getInstance(){
        if(mChannelSDK == null){
            mChannelSDK = new ChannelSDK();
        }
        return mChannelSDK;
    }
    @Override
    public void init(PlatformCallback callback) {
        this.mCallback = mCallback;
    }

    @Override
    public void login() {
        if(mCallback == null){
            toast("请先完成初始化");
        }
        GameCenterSDK.getInstance().doLogin(mActivity, new ApiCallback() {
            @Override
            public void onSuccess(String resultMsg) {
                // 登录成功，调用oppo的接口获取token传给服务端
                GameCenterSDK.getInstance().doGetTokenAndSsoid( new ApiCallback() {
                    @Override
                    public void onSuccess(String resultMsg) {
                        try {
                            JSONObject json = new JSONObject(resultMsg);
                            String token = json.getString("token");
                            String ssoid = json.getString("ssoid");
                            // 拿到token,还得传给服务端校验，校验成功才给登录成功回调
                            toast("登录成功:"+token);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(String resultMsg, int resultCode) {}
                });

            }

            @Override
            public void onFailure(String resultMsg, int resultCode) {
                // 登录失败
                Toast.makeText(mActivity,resultMsg,Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void logout() {

    }

    @Override
    public void onCreate(Activity activity) {
        mActivity = activity;
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onRestart() {

    }

    @Override
    public void onBackPressed() {
        if(mActivity == null){
            //防止空指针崩溃
            return;
        }
        GameCenterSDK.getInstance().onExit(mActivity, new GameExitCallback() {
            @Override
            public void exitGame() {
                mActivity.finish();
            }
        });

    }

    public void toast(Object object){
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(mActivity,object+"",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
